<?php
namespace Home\Controller;
use Home\Controller\IndexController;
class DictionaryController extends IndexController {

    public function lst() {
        //传递页值
        $_p = I('get.p') | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);
        
        $model = D('Dictionary');
        $data = $model->search();
        $this->assign(array(
            'data' => $data['data'],
            'page' => $data['page'],
        ));
        //var_dump($data);
        //var_dump($page);
        $this->display();
    }

    public function add() {
        //传递页值
        $_p = I('get.p') | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);
        
        if (IS_POST) {
            $model = D('Dictionary');
            if ($model->create(I('post.'), 1)) {
                if ($id = $model->add()) {
                    $this->success('添加成功！', U('lst?p=' . $p));
                    exit;
                }
            }
            $this->error($model->getError());
        }
        $model = M('Dictionary');
        $catedata = $model->field('category')->group('category')->select();
        $this->assign('catedata', $catedata);
        $this->display();
    }

    public function addCategory() {
        //传递页值
        $_p = I('get.p') | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);
        
        if (IS_POST) {
            $model = D('Dictionary');
            if ($model->validate($model->_addC)->create(I('post.'), 1)) {
                if ($id = $model->add()) {
                    $this->success('添加成功！', U('lst?p=' . $p));
                    exit;
                }
            }
            $this->error($model->getError());
        }
        $model = M('Dictionary');
        $catedata = $model->field('category')->group('category')->select();
        $this->assign('catedata', $catedata);
        $this->display();
    }

    public function edit() {
        //传递页值
        $_p = I('get.p') | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);
        
        $id = I('get.id');
        if (IS_POST) {
            $model = D('Dictionary');
            if ($model->create(I('post.'), 2)) {
                if ($model->save() !== FALSE) {
                    $this->success('修改成功！', U('lst', array('p' => $p)));
                    exit;
                }
            }
            $this->error($model->getError());
        }
        $model = M('Dictionary');
        $data = $model->find($id);
        $this->assign('data', $data);

        $catedata = $model->field('category')->group('category')->select();
        $this->assign('catedata', $catedata);
        $comeback = U('lst?p=' . $p);
        $this->assign('comeback', $comeback);
        $this->display();
    }

    public function delete() {
        //传递页值
        $_p = I('get.p') | $_p = I('post.p');
        $p = $_p ? $_p : '1';
        $this->assign('p', $p);
        
        $model = D('Dictionary');
        if ($model->delete(I('get.id', 0)) !== FALSE) {
            $this->success('删除成功！', U('lst', array('p' => $p)));
            exit;
        } else {
            $this->error($model->getError());
        }
    }

}
